import uuid
import os
import shutil


def mani():
    count = int(os.listdir("./resources/").__len__()/1000) + 1
    print(count)
    for i in range(count):
        manifest = """
{
    "format_version": 1,
    "header": {
        "description": "A Video Pictures Particle Resource",
        "name": "Minecraft Video Player Generate",
        "uuid": "2281e703-a42d-4683-96eb-e18daa08cdd0",
        "version": [0, 0, 1],
        "min_engine_version": [0, 0, 1]
    },
    "modules": [
        {
            "description": "bgArray",
            "type": "resources",
            "uuid": "2a407c8d-8b0a-44f0-b773-25cc1e81d5ec",
            "version": [0, 0, 1]
        }
    ]
}
        """
        new_manifest = manifest.replace("2281e703-a42d-4683-96eb-e18daa08cdd0", str(uuid.uuid4()))\
            .replace("2a407c8d-8b0a-44f0-b773-25cc1e81d5ec", str(uuid.uuid4()))
        path = "./R" + str(i) + "/"
        try:
            with open(path + "manifest.json", "w") as f:
                f.write(new_manifest)
        except FileNotFoundError:
            # print("T")
            os.mkdir(path)
            with open(path + "manifest.json", "w") as f:
                f.write(new_manifest)

    # ------------------------


def parti():
    C = int(os.listdir("./resources/").__len__() / 1000 + 1)  # + 1
    print(C)
    # n = 0
    for k in range(C):
        particle_json = """
{
    "format_version": "1.10.0",
    "particle_effect": {
        "description": {
            "identifier": "ssssssssssss",
            "basic_render_parameters": {
                "material": "particles_alpha",
                "texture": "kkkkkkkkkkkkkkkk"
            }
        },
        "components": {
            "minecraft:emitter_rate_instant": {
                "num_particles": 1
            },
            "minecraft:emitter_lifetime_once": {
                "active_time": 0.05
            },
            "minecraft:particle_lifetime_expression": {
                "max_lifetime": 0.05
            },
            "minecraft:particle_motion_dynamic": {
                "linear_acceleration": [
                    0,
                    0,
                    0
                ]
            },
            "minecraft:particle_appearance_billboard": {
                "size": [
                    16,
                    9
                ],
                "facing_camera_mode": "direction",
                "uv": {
                    "texture_width": 16,
                    "texture_heigth": 9,
                    "uv": [
                        0,
                        0
                    ],
                    "uv_size": [
                        16,
                        9
                    ]
                }
            }
        }
    }
}"""

        # 图片材质文件夹新建
        try:
            os.makedirs("./R" + str(k) + "/textures/particle/mvp/")
        except FileExistsError:
            pass

        # 图片材质写入
        # m = 0
        count = False
        for i in range(1000):
            try:
                shutil.move("./resources/m" + str(i + 1 + k*1000) + ".png", "./R" + str(k) + "/textures/particle/mvp/")
            except FileNotFoundError:
                count = i
                break
            # except shutil.Error:
            #     pass
            # if m == 1000:
            #     break
            # if ".png" in i:
            #     count += 1
            #     m += 1
            #     shutil.move("./resources/" + i, "./R" + str(k) + "/textures/particle/mvp/")

        # json文件夹新建
        try:
            os.makedirs("./R" + str(k) + "/particles/mvp/")
        except FileExistsError:
            pass

        # json文件写入
        if count is not False:
            for j in range(count):
                print("./R" + str(k) + "/particles/mvp/m" + str(j + 1 + k * 1000) + ".json")
                with open("./R" + str(k) + "/particles/mvp/m" + str(j + 1 + k * 1000) + ".json", 'w') as w:
                    thing = particle_json.replace("kkkkkkkkkkkkkkkk", "textures/particle/mvp/m" +
                                                  str(j + 1 + k * 1000))\
                        .replace("ssssssssssss", "m" + str(j + 1 + k * 1000))
                    w.write(thing)
        else:
            for j in range(1000):
                print("./R" + str(k) + "/particles/mvp/m" + str(j + 1 + k * 1000) + ".json")
                with open("./R" + str(k) + "/particles/mvp/m" + str(j + 1 + k * 1000) + ".json", 'w') as w:
                    thing = particle_json.replace("kkkkkkkkkkkkkkkk", "textures/particle/mvp/m" +
                                                  str(j + 1 + k * 1000))\
                        .replace("ssssssssssss", "m" + str(j + 1 + k * 1000))
                    w.write(thing)


if __name__ == '__main__':
    mani()
    parti()
